package Controller.Actions;

import Controller.*;

import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

import InformationRetriever.IQuery;
import InformationRetriever.QueryFactory;

import Database.DocumentVector;

public class QueryAction implements Action {
    
    public QueryAction() {
    }
    
    public void perform(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        response.setContentType("text/html");
        response.setHeader("Cache-Control", "no-cache");
        
        String query = request.getParameter("query");
        String formula = request.getParameter("formula");
        String strN = request.getParameter("n");
        
        if(query == null || formula == null || strN == null)
        {
            RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");
            rd.forward(request, response);
        }
        
        int n = 0;
        try
        {
            n = Integer.parseInt(strN);
        }
        catch(Exception e)
        {
            RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");
            rd.forward(request, response);            
        }
        
        IQuery queryMethod = QueryFactory.FactoryMethod(formula);
        
        DocumentVector list = queryMethod.PerformQuery(query);
        list.setSize(n);
        request.setAttribute("list", list);
        
        RequestDispatcher rd = request.getRequestDispatcher("/query/resultquery.jsp");
        rd.forward(request, response);
        //response.getWriter().write(query + " " + formula);
    }
}
